From cd128b29a03e5fb3b633203c2eda9a0e4e614e2a Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Wed, 20 Jan 2021 17:25:24 -0600 Subject: [PATCH] Fix entrypoint discovery --- src/pgwui_common/plugin.py | 10 ++++++++-- tests/test_plugin.py | 22 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/pgwui_common/plugin.py b/src/pgwui_common/plugin.py index 2025be0..8608c1f 100644 --- a/src/pgwui_common/plugin.py +++ b/src/pgwui_common/plugin.py @@ -25,10 +25,16 @@ import pkg_resources +def get_component(module): + '''Return the python distribution (package name) from a module name + ''' + return module.split('.')[0] + + def find_pgwui_components(): '''Return list of all pgwui component names as strings ''' - return [entry_point.resolve().__name__ for entry_point in + return [get_component(entry_point.resolve().__name__) for entry_point in pkg_resources.iter_entry_points('pgwui.components')] @@ -39,5 +45,5 @@ def find_pgwui_check_settings(): check_settings = dict() for entry_point in pkg_resources.iter_entry_points('pgwui.check_settings'): callable = entry_point.resolve() - check_settings[callable.__name__] = callable + check_settings[get_component(callable.__module__)] = callable return check_settings diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 10fdfff..ad638af 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -21,12 +21,14 @@ import pytest +from pgwui_testing import testing from pgwui_common import plugin # Helper classes class MockEntryPoint(): def __init__(self, val): + self.__module__ = val self.__name__ = val def resolve(self): @@ -41,14 +43,29 @@ class MockPkgResources(): return [MockEntryPoint(name) for name in self.entry_points] +# get_component() + +@pytest.mark.unittest +def test_get_component(): + '''Returns the expected value + ''' + result = plugin.get_component('package.module') + assert result == 'package' + + +mock_get_component = testing.make_mock_fixture( + plugin, 'get_component') + + # find_pgwui_components() @pytest.mark.unittest -def test_find_pgwui_components(monkeypatch): +def test_find_pgwui_components(mock_get_component, monkeypatch): '''Returns list of entry points via iter_entry_points() ''' entry_points = ['a', 'b', 'c'] + mock_get_component.side_effect = lambda x: x monkeypatch.setattr( plugin, 'pkg_resources', MockPkgResources(entry_points)) @@ -59,11 +76,12 @@ def test_find_pgwui_components(monkeypatch): # find_pgwui_check_settings @pytest.mark.unittest -def test_find_pgwui_check_settings(monkeypatch): +def test_find_pgwui_check_settings(mock_get_component, monkeypatch): '''Returns a dict, keyed by name, of entry points ''' entry_points = ['a', 'b', 'c'] + mock_get_component.side_effect = lambda x: x monkeypatch.setattr( plugin, 'pkg_resources', MockPkgResources(entry_points)) -- 2.34.1